package com.todo.dao;

import com.todo.model.Todo;
import com.todo.util.DatabaseUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TodoDAO {
    
    public void addTodo(Todo todo) throws SQLException {
        String sql = "INSERT INTO todos (title, description, due_date, user_id) VALUES (?, ?, ?, ?)";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, todo.getTitle());
            pstmt.setString(2, todo.getDescription());
            pstmt.setTimestamp(3, todo.getDueDate() != null ? new Timestamp(todo.getDueDate().getTime()) : null);
            pstmt.setLong(4, todo.getUserId());
            
            pstmt.executeUpdate();
        }
    }
    
    public List<Todo> getAllTodos(Long userId) throws SQLException {
        List<Todo> todos = new ArrayList<>();
        String sql = "SELECT * FROM todos WHERE user_id = ? ORDER BY created_date DESC";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setLong(1, userId);
            
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    Todo todo = new Todo();
                    todo.setId(rs.getLong("id"));
                    todo.setTitle(rs.getString("title"));
                    todo.setDescription(rs.getString("description"));
                    todo.setCompleted(rs.getBoolean("completed"));
                    todo.setCreatedDate(rs.getTimestamp("created_date"));
                    todo.setDueDate(rs.getTimestamp("due_date"));
                    todo.setUserId(rs.getLong("user_id"));
                    todos.add(todo);
                }
            }
        }
        return todos;
    }
    
    public void updateTodo(Todo todo) throws SQLException {
        String sql = "UPDATE todos SET title=?, description=?, completed=?, due_date=? WHERE id=? AND user_id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, todo.getTitle());
            pstmt.setString(2, todo.getDescription());
            pstmt.setBoolean(3, todo.isCompleted());
            pstmt.setTimestamp(4, todo.getDueDate() != null ? new Timestamp(todo.getDueDate().getTime()) : null);
            pstmt.setLong(5, todo.getId());
            pstmt.setLong(6, todo.getUserId());
            
            pstmt.executeUpdate();
        }
    }
    
    public void deleteTodo(Long id, Long userId) throws SQLException {
        String sql = "DELETE FROM todos WHERE id=? AND user_id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setLong(1, id);
            pstmt.setLong(2, userId);
            
            pstmt.executeUpdate();
        }
    }
    
    public Todo getTodoById(Long id) throws SQLException {
        String sql = "SELECT * FROM todos WHERE id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setLong(1, id);
            
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    Todo todo = new Todo();
                    todo.setId(rs.getLong("id"));
                    todo.setTitle(rs.getString("title"));
                    todo.setDescription(rs.getString("description"));
                    todo.setCompleted(rs.getBoolean("completed"));
                    todo.setCreatedDate(rs.getTimestamp("created_date"));
                    todo.setDueDate(rs.getTimestamp("due_date"));
                    todo.setUserId(rs.getLong("user_id"));
                    return todo;
                }
            }
        }
        return null;
    }
}